﻿#region 头注释
/******************************************************************************
文件名称    :   MediaFile.cs
创建日期    :   2011/5/13 6:26:03
功能描述    :   文件信实体类
修改日志    :   
    2011-05-13 06:26  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 文件信实体类
    /// </summary>
    public class MediaFile : EntityBase<MediaFile, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MediaFile()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            name = String.Empty;
            path = String.Empty;
            picVideo = false;
            length = null;
            displayName = null;
            checksum = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override MediaFile CreateNew()
        {
            return new MediaFile();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.MediaFile; }
        }

        /// <summary>
        /// 获取或设置文件名
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                if (entityState == EntityState.Saved && name != value) entityState = EntityState.Changed;
                name = value;
            }
        }
        protected string name;

        /// <summary>
        /// 获取或设置文件存储路径
        /// </summary>
        public string Path
        {
            get { return path; }
            set
            {
                if (entityState == EntityState.Saved && path != value) entityState = EntityState.Changed;
                path = value;
            }
        }
        protected string path;

        /// <summary>
        /// 获取或设置文件类型(0:pic;1:video)
        /// </summary>
        public bool PicVideo
        {
            get { return picVideo; }
            set
            {
                if (entityState == EntityState.Saved && picVideo != value) entityState = EntityState.Changed;
                picVideo = value;
            }
        }
        protected bool picVideo;

        /// <summary>
        /// 获取或设置文件长度
        /// </summary>
        public long? Length
        {
            get { return length; }
            set
            {
                if (entityState == EntityState.Saved && length != value) entityState = EntityState.Changed;
                length = value;
            }
        }
        protected long? length;

        /// <summary>
        /// 获取或设置文件友好名
        /// </summary>
        public string DisplayName
        {
            get { return displayName; }
            set
            {
                if (entityState == EntityState.Saved && displayName != value) entityState = EntityState.Changed;
                displayName = value;
            }
        }
        protected string displayName;

        /// <summary>
        /// 获取或设置文件校验码
        /// </summary>
        public string Checksum
        {
            get { return checksum; }
            set
            {
                if (entityState == EntityState.Saved && checksum != value) entityState = EntityState.Changed;
                checksum = value;
            }
        }
        protected string checksum;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override MediaFile CopyTo(MediaFile entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.name = this.name;
            entity.path = this.path;
            entity.picVideo = this.picVideo;
            entity.length = this.length;
            entity.displayName = this.displayName;
            entity.checksum = this.checksum;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<MediaFile>
        {
            MediaFile entity;
            public DefaultXmlSerializer(MediaFile entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public MediaFile Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Name":
                        entity.name = reader.ReadElementString();
                        break;
                    case "Path":
                        entity.path = reader.ReadElementString();
                        break;
                    case "PicVideo":
                        entity.picVideo = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "Length":
                        entity.length = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "DisplayName":
                        entity.displayName = reader.ReadElementString();
                        break;
                    case "Checksum":
                        entity.checksum = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                writer.WriteElementString("Name", entity.Name);
                writer.WriteElementString("Path", entity.Path);
                writer.WriteElementString("PicVideo", XmlConvert.ToString(entity.PicVideo));
                if (entity.Length != null) writer.WriteElementString("Length", XmlConvert.ToString(entity.Length.Value));
                if (entity.DisplayName != null) writer.WriteElementString("DisplayName", entity.DisplayName);
                if (entity.Checksum != null) writer.WriteElementString("Checksum", entity.Checksum);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<MediaFile>
        {
            MediaFile entity;
            public DefaultJsonSerializer(MediaFile entity)
                : base()
            {
                this.entity = entity;
            }

            public MediaFile Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "name":
                        entity.name = Convert.ToString(propValue) ?? entity.name;
                        break;
                    case "path":
                        entity.path = Convert.ToString(propValue) ?? entity.path;
                        break;
                    case "picVideo":
                        entity.picVideo = DbConvert.ToNullableBoolean(propValue) ?? entity.picVideo;
                        break;
                    case "length":
                        entity.length = DbConvert.ToNullableInt64(propValue) ?? entity.length;
                        break;
                    case "displayName":
                        entity.displayName = Convert.ToString(propValue) ?? entity.displayName;
                        break;
                    case "checksum":
                        entity.checksum = Convert.ToString(propValue) ?? entity.checksum;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //媒体文件ID
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //文件名
                writer.WritePropertyName("name");
                writer.WriteValue(entity.Name);

                //文件存储路径
                writer.WritePropertyName("path");
                writer.WriteValue(entity.Path);

                //文件类型(0:pic;1:video)
                writer.WritePropertyName("picVideo");
                writer.WriteValue(entity.PicVideo);

                //文件长度
                writer.WritePropertyName("length");
                if (entity.Length == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Length.Value);
                }

                //文件友好名
                writer.WritePropertyName("displayName");
                if (entity.DisplayName == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.DisplayName);
                }

                //文件校验码
                writer.WritePropertyName("checksum");
                if (entity.Checksum == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Checksum);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}